﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;


namespace WebSite.Ajax
{
    public partial class RecommandClick : System.Web.UI.Page
    {
        private string objectid = "";
        private string objecttype = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            objectid = Request.QueryString["objectid"] + "";
            objecttype = Request.QueryString["objecttype"] + "";

            Users uinfo = UserBase.GetCurrentUser();
            if (uinfo == null)
            {
                Response.Write("请先登录");
                return;
            }
            else
            {
                //判断该用户是否已经推荐过
                RecommandHelper rhelper = new RecommandHelper();
                Criteria c = new Criteria();
                c.Add(CriteriaType.Equals, "ObjectID", objectid);
                c.Add(CriteriaType.Equals, "ObjectType", objecttype);
                c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
                int count = rhelper.QueryRecommandCountBySearch(c);
                if (count > 0)//已经推荐过
                {
                    Response.Write("已经推荐过");
                    return;
                }
                else
                {
                    Recommand rinfo = new Recommand();

                    rinfo.ObjectID = objectid;
                    rinfo.ObjectType = Convert.ToInt32(objecttype);
                    rinfo.UsersID = uinfo.ID;
                    rinfo.NickName = uinfo.NickName;

                    rhelper.AddRecommand(rinfo);

                    //修改对应表中的字段值
                    if (objecttype == "1")//博客
                    {
                        Blog binfo = new Blog();
                        BlogHelper bhelper = new BlogHelper();
                        binfo = bhelper.GetBlog(objectid);
                        int recommandcount = binfo.RecommandCount;
                        recommandcount = recommandcount + 1;
                        binfo.RecommandCount = recommandcount;
                        bhelper.EditBlog(binfo);

                        Response.Write(recommandcount.ToString());
                    }
                    else
                    {

                    }
                }  
            }
        }
    }
}